package com.example.vue20220402.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue20220402.pojo.Power;
import com.example.vue20220402.dao.PowerMapper;
import com.example.vue20220402.pojo.Usersinfo;
import com.example.vue20220402.service.IPowerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vue20220402.vo.Constant;
import com.example.vue20220402.vo.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-04-11
 */
@Service
public class PowerServiceImpl  implements IPowerService {

    @Resource
    PowerMapper powerMapper;

    @Override
    public ResultData list(Power power) {
        ResultData rd = new ResultData();
        try{
            QueryWrapper<Power> qw = new QueryWrapper();
            if(power.getPowerName() != null && !power.getPowerName().equals("")){
                qw.like("power_name",power.getPowerName());
            }
            qw.orderByAsc("power_id");
            Page<Power> page = new Page<>(power.getPage(),power.getRow());
            //List<Usersinfo> list = usersinfoMapper.selectList(qw);
            Page<Power> powerPage = powerMapper.selectPage(page, qw);
            rd.setResult(powerPage);
            rd.setMessage(Constant.USERLIST.getDesc());
            rd.setCode(Constant.OK.getCode());
        }catch (Exception e)
        {
            e.printStackTrace();
            rd.setCode(Constant.ERROR.getCode());
            rd.setMessage(Constant.ERROR.getDesc());
        }
        return rd;
    }

    @Override
    public ResultData updatePower(Power power) {
        QueryWrapper<Power> qw = new QueryWrapper();
        qw.eq("power_id",power.getPowerId());
        int flag = powerMapper.update(power, qw);
        ResultData rd = new ResultData();
        if(flag > 0){
            rd.setCode(Constant.OK.getCode());
        }else{
            rd.setCode(Constant.ERROR.getCode());
        }
        return rd;
    }

    @Override
    public ResultData deletePower(Power power) {
        ResultData rd = new ResultData();
        int flag = powerMapper.deleteById(power.getPowerId());
        if(flag > 0){
            rd.setCode(Constant.OK.getCode());
        }else{
            rd.setCode(Constant.ERROR.getCode());
        }
        return rd;
    }

    @Override
    public ResultData addPower(Power power) {
        ResultData rd = new ResultData();
        QueryWrapper<Power> qw = new QueryWrapper();
        qw.eq("power_name",power.getPowerName());
        Power power2 = powerMapper.selectOne(qw);
        if(power2 != null){
            rd.setCode(Constant.ERROR.getCode());
            rd.setMessage("权限重名了");
            return rd;
        }
        int flag = powerMapper.insert(power);
        if(flag > 0){
            rd.setCode(Constant.OK.getCode());
        }else{
            rd.setCode(Constant.ERROR.getCode());
        }
        return rd;
    }

    @Override
    public ResultData levellist(Power power) {
        ResultData rd = new ResultData();
        QueryWrapper<Power> qw = new QueryWrapper();
        if(power.getPowerIndex() != null){
            qw.eq("power_index",power.getPowerIndex());
        }
        List<Power> powers = powerMapper.selectList(qw);
        rd.setResult(powers);
        return rd;
    }

    @Override
    public ResultData checklevellist(Power power) {
        ResultData rd = new ResultData();
        List<Integer> powers = powerMapper.checklevellist(power);
        rd.setResult(powers);
        return rd;
    }
}
